install.packages("ggplot2")
install.packages("gcookbook")
library(ggplot2)
da<-read.table("C:/Users/Mr.he/Desktop/新建 文本文档.txt",header=T)
pdf("C:/Users/Mr.he/Desktop/out1.pdf",height = 6,width = 8)
ggplot(data=da,mapping=aes(x=height,y=weight))+geom_point(shape=0)
dev.off()
library(gcookbook)
head(heightweight)
ggplot(data=heightweight,mapping=aes(x=ageYear,y=heightIn))+geom_point(shape=16)
ggplot(data=heightweight,mapping=aes(x=ageYear,y=heightIn,size = weightLb))+geom_point(shape=21,alpha =1)+labs(title = "Scatterplot of Weight vs MPG",subtitle = "Data from mtcars", caption = "Source: R datasets")
ggplot(data=heightweight,aes(x=ageYear,y=heightIn))+geom_line() #线性图
#平滑曲线                                                                                                          
ggplot(data=heightweight,aes(x=ageYear,y=heightIn))+geom_smooth(method = "lm")#线性拟合                                                                                                         
#多组数据，分性别展示
ggplot(data=heightweight,aes(x=ageYear,y=heightIn,col=sex))+geom_line()
#同时展示线图，平滑图
ggplot(data=heightweight,aes(x=ageYear,y=heightIn,col=sex))+geom_line(alpha=0.5)+geom_smooth()
#展示男女性人数
ggplot(data=heightweight,aes(x=sex))+geom_bar()
#条形图展示不同date的weight
ggplot(cabbage_exp,aes(x=Date,y=Weight))+geom_bar(stat = "identity")
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill = Cultivar))+geom_bar(stat = "identity")
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill = Cultivar))+geom_bar(stat = "identity",position = "dodge")+geom_text(aes(label = Weight),vjust=2.0,position = position_dodge(1))
#用直方图展示身高分布
ggplot(heightweight,aes(x=heightIn))+geom_histogram(bins=50)#bins控制数据分为多少份 
#用密度分布图展示身高分布
ggplot(heightweight,aes(x=heightIn))+geom_density(fill="blue",alpha=0.1)
ggplot(heightweight,aes(x=heightIn,col=sex))+geom_density(fill="blue",alpha=0.1)
#箱型图
ggplot(heightweight,aes(x=sex,y=heightIn,fill=sex))+geom_boxplot(fill="blue",alpha=0.1)
ggplot(heightweight,aes(x=sex,y=heightIn,fill=sex))+geom_violin(fill="blue",alpha=0.1)
#相关性，热图
head(mtcars)
mcor<-cor(mtcars)
mcor
install.packages("corrplot")
library(corrplot)
corrplot(mcor)
corrplot(mcor,method ="shade",shade.col = NA)#方框阴影展示
corrplot(mcor,method ="shade",shade.col = NA,order="hclust")#层次聚类
corrplot(mcor,method ="shade",shade.col = NA,order="hclust",addCoef.col="black")#添加数值
#
install.packages("heatmap3")
library(heatmap3)
heatmap3(mcor)
heatmap3(mcor,Rowv = NA)#取消行聚类
heatmap3(mcor,Rowv = NA,Colv = NA)#取消所有聚类
#
head(uspopage)
ggplot(uspopage,aes(x=Year,y=Thousands,full=AgeGroup))+geom_area(position ="fill")
#地图
install.packages("mapdata")
library(mapdata)
map("worldHires",regions=c("India"),col="red")
#融合
library(gcookbook)
head(heightweight)
ggplot(heightweight,aes(x=sex,y=heightIn))+theme_bw()+geom_line()+geom_boxplot(aes(fill=sex))
ggplot(heightweight,aes(x=sex,y=heightIn,fill=sex))+theme_bw()+geom_violin()+geom_boxplot(width=1,fill="grey",outlier.color = NA)+stat_summary(fun.y=mean,geom="point",fill="red",size=10)
#
ggplot(mpg,aes(x=displ,y=hwy))+theme_bw()+geom_point()+facet_grid(-cyI,scales="free")
